dotConnect for PostgreSQL Documentation
Devart.Data.PostgreSql Namespace / PgSqlDataReader Class / GetBytes Method / GetBytes(Int32,Int64,Byte[],Int32,Int32) Method
The zero-based column ordinal.
The index within the field where the read operation is to begin.
The buffer into which to read the stream of bytes.
The index within the buffer where the write operation is to begin.
The maximum length to copy into the buffer.
Example

GetBytes(Int32,Int64,Byte[],Int32,Int32) Method
Reads a stream of bytes from the specified column offset into the buffer as an array starting at the given buffer offset.
Syntax
'Declaration
 
Public Overloads Overrides Function GetBytes( _
   ByVal i As Integer, _
   ByVal fieldOffset As Long, _
   ByVal buffer() As Byte, _
   ByVal bufferOffset As Integer, _
   ByVal length As Integer _
) As Long
 

Parameters

i
The zero-based column ordinal.
fieldOffset
The index within the field where the read operation is to begin.
buffer
The buffer into which to read the stream of bytes.
bufferOffset
The index within the buffer where the write operation is to begin.
length
The maximum length to copy into the buffer.

Return Value

The actual number of bytes read.

Remarks
GetBytes returns the number of available bytes in the field. In most cases this is the exact length of the field. However, the number returned may be less than the true length of the field if GetBytes has already been used to obtain bytes from the field. This may be the case, for example, if the PgSqlDataReader is reading a large data structure into a buffer.

If you pass a buffer that is a null value, GetBytes returns the length of the field in bytes.

Example
This sample shows how to obtain array of bytes from a table and save it to a file.
static void GetThatBytes(PgSqlConnection pgConnection)
{
  PgSqlCommand cmd = new PgSqlCommand("SELECT * FROM Test.Dept");
  cmd.Connection = pgConnection;
  pgConnection.Open();
  try
  {
    PgSqlDataReader reader = cmd.ExecuteReader();
    reader.Read();
    byte[] myBytes = new byte[50];
    long bytesRead = reader.GetBytes(reader.GetOrdinal("DName"),0,myBytes,0,50);
    FileStream fs = new FileStream("D:\\Tmp\\1.txt", FileMode.Create);
    fs.Write(myBytes,0,(int)bytesRead);
    fs.Close();
    Console.WriteLine(bytesRead+ " bytes downloaded from table to file.");
    reader.Close();
  }
  finally
  {
    pgConnection.Close();
  }
}
Public Sub GetThatBytes(ByVal pgConnection As PgSqlConnection)
  Dim cmd As PgSqlCommand = New PgSqlCommand("SELECT * FROM Test.Dept")
  cmd.Connection = pgConnection
  pgConnection.Open()
  Try
    Dim reader As PgSqlDataReader = cmd.ExecuteReader()
    reader.Read()
    Dim myBytes(50) As Byte
    Dim bytesRead As Long = reader.GetBytes(reader.GetOrdinal("DName"), 0, myBytes, 0, 50)
    Dim fs As FileStream = New FileStream("D:\Tmp\1.txt", FileMode.Create)
    fs.Write(myBytes, 0, Convert.ToInt32(bytesRead))
    fs.Close()
    Console.WriteLine(bytesRead & " bytes downloaded from table to file.")
    reader.Close()
  Finally
    pgConnection.Close()
  End Try
End Sub
Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also